package com.luzhikun.shoppingCar;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/*
 * @Author liu-miss
 * @Description 自制实现购物车(session)
 * @Date 14:41 2021/5/7
 **/
@WebServlet(name = "ShoppingCarServlet", value = "/shoppingCarServlet")
public class ShoppingCarServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 设置字符集
        request.setCharacterEncoding("UTF-8");

        // 获取客户端传来的数据
        String name = request.getParameter("name");
        String price = request.getParameter("price");

        // 向Tomcat索要当前用户的私人储物柜
        HttpSession session = request.getSession();
        Integer attribute = (Integer) session.getAttribute(name);
        if (attribute==null) {
            session.setAttribute(name, 1);
        }else {
            session.setAttribute(name,attribute+1);
        }

    }
}
